﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TaoNiuOA.Web
{
    /// <summary>
    /// 权限属性类,判断是否已经登录
    /// </summary>
    public class IsLogon : FilterAttribute, IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            var db = new TaoNiuOA.Models.DbEntity();
            var user = db.Users.SingleOrDefault(n => n.UserName == "ggshihai");
            if (user == null)
            {
                //测试用
                //事务,保持数据一致
                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope())
                {
                    //初始化用户信息
                    var userinfo = new TaoNiuOA.Models.UserInfo();
                    db.UserInfoes.Add(userinfo);
                    db.SaveChanges();

                    //用户权限
                    var userRole = new TaoNiuOA.Models.UserRole();
                    userRole.Apply = false;
                    userRole.Department = false;
                    userRole.TaskAdd = false;
                    userRole.TaskApprove = false;
                    userRole.User = false;
                    userRole.UserGroup = false;
                    db.UserRoles.Add(userRole);
                    db.SaveChanges();

                    user = new Models.User();
                    user.Email = "xshai@qq.com";
                    user.LastActivityTime = DateTime.Now;
                    user.Password = ShenLan.Common.Encrypt.EnctyptPassword("000000");
                    user.PasswordAnswer = "000000";
                    user.PasswordQuestion = "000000";
                    user.RegTime = DateTime.Now;
                    user.Status = 1;
                    user.UserGroupID = 0;
                    user.UserInfoID = userinfo.UserInfoID;
                    user.UserRoleID = userRole.UserRoleID;
                    user.UserName = "ggshihai";
                    db.Users.Add(user);
                    db.SaveChanges();
                    ts.Complete();
                }
            }
            filterContext.HttpContext.Session["users"] = user;

            if (filterContext.HttpContext.Session["users"] == null)
            {
                var returnUrl = filterContext.HttpContext.Request.Url.AbsoluteUri;
                var errorMsg = HttpUtility.UrlEncode("请登录再继续进行相关操作！");
                var url = string.Format("/user/logon?returnUrl={0}&error={1}", returnUrl, errorMsg);
                filterContext.Result = new RedirectResult(url);
            }
            //else
            //{
            //    var returnUrl = filterContext.HttpContext.Request.Url.AbsoluteUri.ToLower();
            //    if (returnUrl.IndexOf("myinfo") == -1)
            //    {
            //        var user = filterContext.HttpContext.Session["users"] as TaoNiuOA.Models.User;
            //        var userInfo = user.GetUserInfo();
            //        if (string.IsNullOrEmpty(userInfo.Name))
            //        {
            //            filterContext.HttpContext.Response.Write("<script>alert('您得至少完善您的个人真实姓名等信息才能继续操作');</script>");
            //            var url = "/ucenter/myinfo";
            //            filterContext.Result = new RedirectResult(url);
            //        }
            //    }
            //}
        }
    }

    /// <summary>
    /// 权限属性类,判断是否已经登录
    /// </summary>
    public class IsAdmin : FilterAttribute, IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            var db = new TaoNiuOA.Models.DbEntity();
            var user = db.Users.SingleOrDefault(n => n.UserName == "ggshihai");
            filterContext.HttpContext.Session["users"] = user;

            if (user == null)
            {
                //测试用
                //事务,保持数据一致
                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope())
                {
                    //初始化用户信息
                    var userinfo = new TaoNiuOA.Models.UserInfo();
                    db.UserInfoes.Add(userinfo);
                    db.SaveChanges();

                    //用户权限
                    var userRole = new TaoNiuOA.Models.UserRole();
                    userRole.Apply = false;
                    userRole.Department = false;
                    userRole.TaskAdd = false;
                    userRole.TaskApprove = false;
                    userRole.User = false;
                    userRole.UserGroup = false;
                    db.UserRoles.Add(userRole);
                    db.SaveChanges();

                    user = new Models.User();
                    user.Email = "xshai@qq.com";
                    user.LastActivityTime = DateTime.Now;
                    user.Password = ShenLan.Common.Encrypt.EnctyptPassword("000000");
                    user.PasswordAnswer = "000000";
                    user.PasswordQuestion = ShenLan.Common.Encrypt.EnctyptPassword("000000");
                    user.RegTime = DateTime.Now;
                    user.Status = 1;
                    user.UserGroupID = 0;
                    user.UserInfoID = userinfo.UserInfoID;
                    user.UserRoleID = userRole.UserRoleID;
                    user.UserName = "ggshihai";
                    db.Users.Add(user);
                    db.SaveChanges();
                    ts.Complete();
                }
            }

            if (filterContext.HttpContext.Session["users"] == null)
            {
                var returnUrl = filterContext.HttpContext.Request.Url.AbsoluteUri;
                var errorMsg = HttpUtility.UrlEncode("请登录再继续进行相关操作！");
                var url = string.Format("/user/logon?returnUrl={0}&error={1}", returnUrl, errorMsg);
                filterContext.Result = new RedirectResult(url);
            }
        }
    }
}